package com.flmes.base.config;

import com.flmes.base.base.model.UserToken;
import com.flmes.base.constant.RequestConstant;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @Author: flmes
 * @Date: 2024/10/9 22:01
 */
@Component
public class UserTokenHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(UserToken.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        // 将当前登录信息注入接口参数
        return webRequest.getAttribute(RequestConstant.USER_INFO, RequestAttributes.SCOPE_REQUEST);
    }

}
